<template>
  <div class="container">
    <div class="app-container">
        <div class="input-btns">
          <el-input
            v-model="templateInputContent"
            style="margin-bottom:10px"
            type="text"
            size="small"
            placeholder="输入模版名称"
          />
          <el-button 
            @click="addTemplate"
            style="background-color: cornflowerblue; color: black;"
            type="button"
            size="small"
          >添加</el-button>
        </div>
        <div class="table-container">
          <el-table
            ref="templateTable"
            :data="templateList"
            @current-change="selectNode"
            :cell-style="cellStyle"
            height="600px"
          >
          <el-table-column
            property="name"
            label="模版名称"
            width="350px">
          </el-table-column> 
          <el-table-column label="操作" width="300px" fixed="right">
            <template v-slot="{ row }">
              <el-button slot="reference" style="margin-left:10px" type="text" @click="showExcelDialog = true">上传</el-button>
              <el-button slot="reference" style="margin-left:10px" type="text" :disabled="!row.disabled" @click="doT(row.id)">查看</el-button>
              <el-popconfirm
                title="确认删除该行数据吗？"
                @onConfirm="confirmDel(row.id)"
              >
                <el-button slot="reference" style="margin-left:10px" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        </div>
        <import-excel :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="showInfo" />
      </div>
  </div>
</template>
<script>
import { getTemplateList, addTemplate, downTemplate, getFileName, delTemplate } from '@/api/employee'
import ImportExcel from './components/import-excel.vue'
import store from '@/store'
import FileSaver from 'file-saver'
export default {
  name: 'Salary',
  components: {
    ImportExcel
  },
  data() {
    return {
      templateInputContent: '',
      templateList: [],
      showExcelDialog: false,
      templateId: '',
    }
  },
  created() {
    this.getTemplate()
  },
  methods: {
    async getTemplate() {
      const data = await getTemplateList()
      this.templateList = data
    },
    async addTemplate() {
      if (!this.templateInputContent) {
        this.$message.error('请输入模版名称！')
        return
      }
      const data = {
        name: this.templateInputContent
      }
      await addTemplate(data)
      this.templateInputContent = ''
      this.getTemplate()
    },
    selectNode(node) {
      this.templateId = node.id
    },
    cellStyle() {
      return "cursor:pointer;"
    },
    async confirmDel(id) {
      await delTemplate(id)
      this.getTemplate()
    },
    showInfo() {
      this.getTemplate() // 刷新一下
      this.$message.success('上传成功！')
    },
    async doT(id) {
      const name = await getFileName(id)
      const data = await downTemplate(id)
      FileSaver.saveAs(data,name)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  margin-left: 30px;
  width: 1000px;
  height: 650px;
  background: #fff;
  .input-btns{
    height: 32px;
    display: flex;
    flex-direction: row;
  }
  .table-container{
    height: 800px;
  }
  
}
:deep(.el-input){
  width:348px;
  margin-left:8px
}
</style>